<template>
  <div class="app-container">
    <el-card>
      <div slot="header">
        <div class="tools-select">
          <el-row>
            <el-col :span="24">
              <el-form
                :inline="true"
                :model="form"
                @submit.native.prevent="handleQuery()"
              >
                <el-form-item>
                  <el-input
                    v-model="form.name"
                    placeholder="名称"
                    size="mini"
                    clearable
                    status-icon
                    @clear="handleQuery"
                  />
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-search"
                    native-type="submit"
                  >查询
                  </el-button>
                </el-form-item>
                <el-form-item>
                  <el-button
                    type="info"
                    size="mini"
                    icon="el-icon-plus"
                    @click="handleAdd"
                    v-if="$store.getters.roles.includes('SYS_CONFIG_ADD')"
                  >新增
                  </el-button>
                </el-form-item>
              </el-form>
            </el-col>
          </el-row>
        </div>
      </div>
      <!-- 表格区 -->
      <el-table size="mini" stripe :data="tableData" border>
        <el-table-column align="center" label="序号" type="index" width="50px"/>
        <el-table-column align="center" label="配置名称" prop="name"/>
        <el-table-column align="center" label="配置值" prop="val"/>
        <el-table-column align="center" label="创建时间" prop="createTime"/>
        <el-table-column align="center" label="更新时间" prop="updateTime"/>
        <el-table-column align="center" label="操作" width="300">
          <template v-slot="scope">
            <el-button
              type="text"
              size="mini"
              icon="el-icon-view"
              @click="handleView(scope.row.id)"
            >查看
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_CONFIG_EDIT')"
            >编辑
            </el-button>
            <el-button
              type="text"
              size="mini"
              icon="el-icon-delete"
              @click="handleDel(scope.row.id)"
              v-if="$store.getters.roles.includes('SYS_CONFIG_DELETE')"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区 -->
      <el-pagination
        :current-page="this.page.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>
    <ViewDialog ref="view"/>
    <EditDialog ref="edit" @onCallBack="handleQuery"/>
  </div>
</template>

<script>
import { getPage, deleteById } from '@/api/sysConfig'
import ViewDialog from './components/ViewDialog'
import EditDialog from './components/EditDialog'

export default {
  components: { EditDialog, ViewDialog },
  data() {
    return {
      tableData: [],
      page: {
        pageSize: 10,
        pageNum: 1,
        total: undefined
      },
      form: {
        name: ''
      }
    }
  },
  mounted() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      getPage({
        pageSize: this.page.pageSize,
        pageNum: this.page.pageNum,
        name: this.form.name
      }).then((res) => {
        this.tableData = res.data.records
        this.page.total = res.data.total
      })
    },
    handleSizeChange(pageSize) {
      this.page.pageSize = pageSize
      this.handleQuery()
    },
    handleCurrentChange(pageNum) {
      this.page.pageNum = pageNum
      this.handleQuery()
    },
    handleView(id) {
      this.$refs['view'].onShow(id)
    },
    handleAdd() {
      this.$refs['edit'].onShow()
    },
    handleEdit(id) {
      this.$refs['edit'].onShow(id)
    },
    handleDel(id) {
      deleteById(id).then(res => {
        if (res.code === 20000) {
          this.handleQuery()
          this.$message({
            message: '操作成功',
            type: 'success'
          })
        }
      })
    }
  }
}
</script>
<style scoped>

.el-form {
  margin-top: 20px;
}

.el-pagination {
  float: left;
  padding-top: 20px;
  padding-bottom: 20px;
}

.tools-select {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #ffffff;
  padding: 10px;
  border: 1px;
  border-radius: 5px;
}

</style>
